package com.changgou.search.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private ESManagerMapper esManagerMapper;


    /**
     * @Description:    创建索引库结构
     * @Author:        wangqingsong
     * @param:           []
     * @return:          void
     * @CreateDate:     2020/11/27 14:41
     */
    @Override
    public void createMappingAndIndex() {
        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);
        //创建映射
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }

    /**
     * @Description:    导入全部数据进入es
     * @Author:        wangqingsong
     * @param:           []
     * @return:          void
     * @CreateDate:     2020/11/27 15:15
     */
    @Override
    public void importAll() {
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        if (CollUtil.isEmpty(skuList)) {
            throw new RuntimeException("当前没有数据查询到,无法导入索引库");
        }

        String jsonSkuList = JSON.toJSONString(skuList);

        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);
        for (SkuInfo skuInfo : skuInfoList) {
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }
        //导入索引库
        esManagerMapper.saveAll(skuInfoList);

    }


    /**
     * @Description:    根据spuId查询SkuList，再导入索引库
     * @Author:        wangqingsong
     * @param:           [spuId]
     * @return:          void
     * @CreateDate:     2020/11/27 15:36
     */
    @Override
    public void importDataBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (CollUtil.isEmpty(skuList)) {
            throw new RuntimeException("当前没有数据查询到,无法导入索引库");
        }
        String jsonSkuList = JSON.toJSONString(skuList);
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }

        esManagerMapper.saveAll(skuInfoList);
    }


    /**
     * @Description:    根据spuId删除索引库中相关的sku数据
     * @Author:        wangqingsong
     * @param:           [spuId]
     * @return:          void
     * @CreateDate:     2020/11/28 16:36
     */
    @Override
    public void deleteDataBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (CollUtil.isEmpty(skuList)) {
            throw new RuntimeException("当前未查询到数据,无法导入索引库");
        }

        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }
    }

}
